import { Component, OnInit } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';
import { Service } from 'app/server/server';

import { app } from '../application.data';
import { appUrl } from '../application.url';

declare var $:any;

@Component({
  selector: 'application-table',
  templateUrl: './application-table.component.html',
  styleUrls: ['./application-table.component.css'],
})


export class ApplicationTableComponent implements OnInit {

  tabVal     : Array<object>;
  total      : number = 0;
  naturalNum : number = 0;

  isSelectAll : boolean = false;
  isChose     : boolean = false;

  right: any = { "system" : {}, "application" : {} };
  constructor(
    public initService: InitService,
    public request: CudTableService, 
    public removeTableService: RemoveTableService,
    private hintsService: HintsService,
    private server:Service
  ) 
  {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {   
  }

  /**
   * 表格
   */ 
  _table() {
    this.request
        .postRequest(appUrl.getList, this.tableData())
        .then(data=> {
          if(data.result_code == '0000') {
            this.tabVal     = data.result;
            this.total      = data.length;
            this.naturalNum = app.pagination.naturalNum;
          }else {
            this.hintsService._operationHints(data.message, data.result_code);
          }
          this._cancelChecked();
          return
        })
        .catch(err=> {
          this.hintsService._operationHints('error', 'error');
          this._cancelChecked();
          return
        });
    }

  /**
   * 全选
   * @param bool 
   * @param tabVal 
   */ 
  _selectAll(bool, tabVal) {  
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'appId');
    if (this.isSelectAll == false) {
      this._table();
    }
  }

  /**
   * 单选（多选）
   * @param bool 
   * @param appId 
   * @param length 
   */ 
  _chose(bool, appId, length) {  
    this.isSelectAll = this.removeTableService.removeChose(bool, appId, length)['bool'];
  }

  /**
   * 重置表格复选框、选中的ID
   */ 
  private _cancelChecked() {
    this.isChose = this.isSelectAll = false;
    this.removeTableService._clearId('appId');
  }

  /************************ 提交给后端的数据 ************************/

  /**
   * 表格
   */ 
  private tableData() {
    return { 
      institutionId   : app.tree.institutionId,
      pageNumber      : app.pagination.pageNumber,
      pageSize        : app.pagination.pageSize,
      queryType       : app.retrieval.queryType,
      startCreateTime : app.retrieval.startCreateTime,
      endCreateTime   : app.retrieval.endCreateTime,
      queryCriteria   : app.retrieval.queryCriteria
    }
  }

}





